﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MadProps.Tests.PropChangedTests
{
    [TestClass]
    public class can_handle_two_props_referencing_the_same_child
    {
        public class Parent : BaseModel
        {
            public readonly IProp<Child> FirstChild;
            public readonly IProp<Child> SecondChild;
        }

        public class Child : BaseModel
        {
            public readonly IProp<int> AnInt;
        }

        [TestMethod]
        public void MyTestMethod()
        {
            var parent = new Parent();
            var child = new Child();

            int count = 0;

            parent.FirstChild.Value = child;
            Assert.AreEqual(1, parent.Recorder.ChangeCountFor(parent.FirstChild));
            Assert.AreEqual(++count, parent.Recorder.Total);

            parent.SecondChild.Value = child;
            Assert.AreEqual(1, parent.Recorder.ChangeCountFor(parent.SecondChild));
            Assert.AreEqual(++count, parent.Recorder.Total);

            child.AnInt.Value = 784;
            Assert.AreEqual(1, parent.Recorder.ChangeCountFor(child.AnInt));
            Assert.AreEqual(++count, parent.Recorder.Total);

            // Now this is important!
            // When SecondChild changes the parent needs to stay subscribed (via FirstChild)
            parent.SecondChild.Value = null;
            Assert.AreEqual(++count, parent.Recorder.Total);

            child.AnInt.Value = 9702;
            Assert.AreEqual(2, parent.Recorder.ChangeCountFor(child.AnInt));
            Assert.AreEqual(++count, parent.Recorder.Total);

            // And when FirstChild changes there is no longer a path from parent to child
            parent.FirstChild.Value = null;
            Assert.AreEqual(++count, parent.Recorder.Total);

            child.AnInt.Value = 1365;
            Assert.AreEqual(2, parent.Recorder.ChangeCountFor(child.AnInt));
            Assert.AreEqual(count, parent.Recorder.Total);
        }
    }
}
